/**
 * validate email
 * @param email
 * @returns {boolean}
 */
export function validateEmail(email) {
  const re =
    /^(([^<>()\\[\]\\.,;:\s@"]+(\.[^<>()\\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return re.test(email);
}

class Storage {
  constructor({ PREFIX = "" } = {}) {
    this.keyName = PREFIX;
  }

  /**
   * 存储localStorage
   */
  setStore = (params = {}) => {
    let { name, content, type, day = 0, version = null } = params;
    name = this.keyName + name;
    let obj = {
      dataType: typeof content,
      content: content,
      type: type,
      day,
      datetime: day * 24 * 3600 * 1000 + new Date().getTime(),
      version,
    };
    if (type) window.sessionStorage.setItem(name, JSON.stringify(obj));
    else window.localStorage.setItem(name, JSON.stringify(obj));
  };

  // 判断是否新版本的存储方式
  isStoreCreate(obj) {
    return obj.hasOwnProperty("dataType") && obj.hasOwnProperty("content");
  }

  /**
   * 获取localStorage
   */
  getStore = (params = {}) => {
    let { name, debug, version } = params;
    name = this.keyName + name;
    let obj = {},
      content;
    obj = window.sessionStorage.getItem(name);
    if (validatenull(obj)) obj = window.localStorage.getItem(name);
    if (validatenull(obj)) return;
    try {
      obj = JSON.parse(obj);

      // 兼容改造前的存储
      if (!this.isStoreCreate(obj)) {
        return obj;
      }
      // 判断 缓存的版本 不等于 当前版本就删除
      if (version && obj.version !== version) {
        window.sessionStorage.removeItem(name);
        window.localStorage.removeItem(name);
        return null;
      }
      //
      if (obj.day) {
        if (obj.datetime <= new Date().getTime()) {
          window.sessionStorage.removeItem(name);
          window.localStorage.removeItem(name);
          return null;
        }
      }
    } catch {
      return obj;
    }
    if (debug) {
      return obj;
    }
    if (obj.dataType == "string") {
      content = obj.content;
    } else if (obj.dataType == "number") {
      content = Number(obj.content);
    } else if (obj.dataType == "boolean") {
      content = eval(obj.content);
    } else if (obj.dataType == "object") {
      content = obj.content;
    }
    return content;
  };

  /**
   * 删除localStorage
   */
  removeStore = (params = {}) => {
    let { name, type } = params;
    name = this.keyName + name;
    if (type) {
      window.sessionStorage.removeItem(name);
    } else {
      window.localStorage.removeItem(name);
    }
  };

  /**
   * 获取全部localStorage
   */
  getAllStore = (params = {}) => {
    let list = [];
    let { type } = params;
    if (type) {
      for (let i = 0; i <= window.sessionStorage.length; i++) {
        list.push({
          name: window.sessionStorage.key(i),
          content: getStore({
            name: window.sessionStorage.key(i),
            type: "session",
          }),
        });
      }
    } else {
      for (let i = 0; i <= window.localStorage.length; i++) {
        list.push({
          name: window.localStorage.key(i),
          content: getStore({
            name: window.localStorage.key(i),
          }),
        });
      }
    }
    return list;
  };

  /**
   * 清空全部localStorage
   */
  clearStore = (params = {}) => {
    let { type } = params;
    if (type) {
      window.sessionStorage.clear();
    } else {
      window.localStorage.clear();
    }
  };
}

export default Storage;
